<?php


namespace app\seller\controller;

use think\Db;


class Staff extends Base
{
    public function index()
    {
        $length = input('length');
        $list = Db::name('staff')->where('category_id', $this->shop_id)->limit($length, 30)->select();
        foreach ($list as &$item) {
            $item['avatar'] = request()->domain() . $item['avatar'];
        }
        return $this->success('获取成功', $list);
    }

    public function edit()
    {
        $id = input('id');
        $data = Db::name('staff')->where('category_id', $this->shop_id)->where('id', $id)->find();
        if($data){
            $data['avatar'] = request()->domain() . $data['avatar'];
        }
        return $this->success('获取成功', $data);
    }

    public function add()
    {
        $avatar = input('avatar');
        if (!$avatar) {
            return $this->error('请输入上传头像');
        }
        $nickname = input('nickname');
        if (!$nickname) {
            return $this->error('请输入名称');
        }
        $username = input('username');
        if (!$username) {
            return $this->error('请输入登录账号');
        }
        if (Db::name('staff')->where('username', $username)->count()) {
            return $this->error('登录账号已存在');
        }
        $password = input('password');
        if (!$password) {
            return $this->error('请输入登录密码');
        }
        $res = Db::name('staff')->insert([
            'category_id' => $this->shop_id,
            'nickname' => $nickname,
            'avatar' => $avatar,
            'create_time' => time(),
            'username' => $username,
            'password' => md5($password),
        ]);
        if ($res !== false) {
            return $this->success('操作成功');
        }
        return $this->error('操作失败');
    }

    public function del()
    {
        $id = input('id');
        $res = Db::name('staff')->where([
            'id' => $id,
            'category_id' => $this->shop_id,
        ])->delete();
        if ($res !== false) {
            return $this->success('操作成功');
        }
        return $this->error('操作失败');
    }

    public function doedit()
    {
        $id = input('id');
        $password = input('password');
        $avatar = input('avatar');
        if (!$avatar) {
            return $this->error('请输入上传头像');
        }
        $nickname = input('nickname');
        if (!$nickname) {
            return $this->error('请输入名称');
        }
        $username = input('username');
        if (!$username) {
            return $this->error('请输入登录账号');
        }
        if (Db::name('staff')->where('username', $username)->where('id', "<>", $id)->count()) {
            return $this->error('登录账号已存在');
        }
        $data['nickname'] = $nickname;
        $data['avatar'] = $avatar;
        $data['username'] = $username;
        if ($password)
            $data['password'] = md5($password);
        $res = Db::name('staff')->where([
            'id' => $id,
            'category_id' => $this->shop_id,
        ])->update($data);
        if ($res !== false) {
            return $this->success('操作成功');
        }
        return $this->error('操作失败');
    }
}
